﻿/**
 * 首页
 */
Ext.define("PSI.Home.MainForm", {
			extend : "Ext.panel.Panel",

			config : {
				pSale : "",
				pInventory : "",
				pPurchase : "",
				pMoney : ""
			},

			border : 0,
			bodyPadding : 5,

			getPortal : function(index) {
				var me = this;
				if (!me.__portalList) {
					me.__portalList = [];
					
					me.__portalList.push(me.getInventoryPortal());
					me.__portalList.push(me.getDailyDetailPortal());
					me.__portalList.push(me.getMonthDetailPortal());
				}

				if (index == 0 && me.__portalList.length == 0) {
					return me.getInfoPortal();
				}

				if (index >= me.__portalList.length || index < 0) {
					return {
						border : 0
					};
				}
				
				return me.__portalList[index];
			},

			initComponent : function() {
				var me = this;
				
				Ext.apply(me, {
							layout : "hbox",
							items : [{
								flex : 1,
								layout : "vbox",
								border : 0,
								items : [
								        me.getPortal(0),
										me.getPortal(1),
										me.getPortal(2)
										]
							}]
						});

				me.callParent(arguments);

				Ext.get(window).on('beforeunload', this.onWindowBeforeUnload);

				me.queryInventoryData();
				me.queryDailyDetailData();
				me.queryMonthDetailData();
			},

			getInventoryGrid : function() {
				var me = this;
				if (me.__inventoryGrid) {
					return me.__inventoryGrid;
				}

				var modelName = "PSIPortalInventory";
				Ext.define(modelName, {
							extend : "Ext.data.Model",
							fields : ["warehouseName", "balance_count"]
						});

				me.__inventoryGrid = Ext.create("Ext.grid.Panel", {
							viewConfig : {
								enableTextSelection : true
							},
							columnLines : true,
							border : 0,
							columns : [{
										header : "仓库",
										dataIndex : "warehouseName",
										width : 160,
										menuDisabled : true,
										sortable : false
									}, {
										header : "商品余额数量",
										dataIndex : "balance_count",
										width : 140,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}],
							store : Ext.create("Ext.data.Store", {
										model : modelName,
										autoLoad : false,
										data : []
									})
						});

				return me.__inventoryGrid;
			},
			
			//每日出入库明细
			getDailyDetailPortalGrid : function() {
				var me = this;
				if (me.__dailyDetailPortalGrid) {
					return me.__dailyDetailPortalGrid;
				}

				var modelName = "PSIPortalDailyDetail";
				Ext.define(modelName, {
							extend : "Ext.data.Model",
							fields : ["warehouseName", "date","in_count", "out_count", "sum"]
						});

				me.__dailyDetailPortalGrid = Ext.create("Ext.grid.Panel", {
							viewConfig : {
								enableTextSelection : true
							},
							columnLines : true,
							border : 0,
							columns : [{
										header : "仓库",
										dataIndex : "warehouseName",
										width : 100,
										menuDisabled : true,
										sortable : false
									}, {
										header : "日期",
										dataIndex : "date",
										width : 80,
										menuDisabled : true,
										sortable : false
									}, {
										header : "出库数量",
										dataIndex : "out_count",
										width : 80,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}, {
										header : "入库数量",
										dataIndex : "in_count",
										width : 80,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}, {
										header : "出入合计",
										dataIndex : "sum",
										width : 80,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}],
							store : Ext.create("Ext.data.Store", {
										model : modelName,
										autoLoad : false,
										data : []
									})
						});

				return me.__dailyDetailPortalGrid;
			},

			//每月出入库明细
			getMonthDetailPortalGrid : function() {
				var me = this;
				if (me.__monthDetailPortalGrid) {
					return me.__monthDetailPortalGrid;
				}

				var modelName = "PSIPortalMonthDetail";
				Ext.define(modelName, {
							extend : "Ext.data.Model",
							fields : ["warehouseName", "month","in_count", "out_count", "sum"]
						});

				me.__monthDetailPortalGrid = Ext.create("Ext.grid.Panel", {
							viewConfig : {
								enableTextSelection : true
							},
							columnLines : true,
							border : 0,
							columns : [{
										header : "仓库",
										dataIndex : "warehouseName",
										width : 100,
										menuDisabled : true,
										sortable : false
									}, {
										header : "月份",
										dataIndex : "month",
										width : 80,
										menuDisabled : true,
										sortable : false
									}, {
										header : "出库数量",
										dataIndex : "out_count",
										width : 80,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}, {
										header : "入库数量",
										dataIndex : "in_count",
										width : 80,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}, {
										header : "出入合计",
										dataIndex : "sum",
										width : 80,
										menuDisabled : true,
										sortable : false,
										align : "right",
										xtype : "numbercolumn",
										format : "0,000.000"
									}],
							store : Ext.create("Ext.data.Store", {
										model : modelName,
										autoLoad : false,
										data : []
									})
						});

				return me.__monthDetailPortalGrid;
			},

			getInventoryPortal : function() {
				var me = this;
				return {
					header : {
						title : "<span style='font-size:120%'>仓库总库存看板</span>",
						iconCls : "GX_PSI-portal-inventory",
						height : 40
					},
					flex : 1,
					width : "100%",
					height : 100,
					margin : "5",
					layout : "fit",
					items : [me.getInventoryGrid()]
				};
			},
			
			//每日明细
			getDailyDetailPortal : function() {
				var me = this;
				return {
					header : {
						title : "<span style='font-size:120%'>日出入库汇总</span>",
						iconCls : "GX_PSI-portal-daycounter",
						height : 40
					},
					flex : 1,
					width : "100%",
					height : 210,
					margin : "5",
					layout : "fit",
					items : [me.getDailyDetailPortalGrid()]
				};
			},
			
			//每月明细
			getMonthDetailPortal : function() {
				var me = this;
				return {
					header : {
						title : "<span style='font-size:120%'>月出入库汇总</span>",
						iconCls : "GX_PSI-portal-daycounter",
						height : 40
					},
					flex : 1,
					width : "100%",
					height : 210,
					margin : "5",
					layout : "fit",
					items : [me.getMonthDetailPortalGrid()]
				};
			},
			
			queryInventoryData : function() {
				var me = this;
				var grid = me.getInventoryGrid();
				var el = grid.getEl() || Ext.getBody();
				el.mask(PSI.Const.LOADING);
				Ext.Ajax.request({
							url : PSI.Const.BASE_URL
									+ "Home/Portal/inventoryPortal",
							method : "POST",
							callback : function(options, success, response) {
								var store = grid.getStore();
								store.removeAll();

								if (success) {
									var data = Ext.JSON
											.decode(response.responseText);
									store.add(data);
								}

								el.unmask();
							}
						});
			},
			
			queryDailyDetailData : function() {
				var me = this;
				var grid = me.getDailyDetailPortalGrid();
				var el = grid.getEl() || Ext.getBody();
				el.mask(PSI.Const.LOADING);
				Ext.Ajax.request({
							url : PSI.Const.BASE_URL
									+ "Home/Portal/dailyDetailPortal",
							method : "POST",
							callback : function(options, success, response) {
								var store = grid.getStore();
								store.removeAll();

								if (success) {
									var data = Ext.JSON
											.decode(response.responseText);
									store.add(data);
								}

								el.unmask();
							}
						});
			},
			
			queryMonthDetailData : function() {
				var me = this;
				var grid = me.getMonthDetailPortalGrid();
				var el = grid.getEl() || Ext.getBody();
				el.mask(PSI.Const.LOADING);
				Ext.Ajax.request({
							url : PSI.Const.BASE_URL
									+ "Home/Portal/monthDetailPortal",
							method : "POST",
							callback : function(options, success, response) {
								var store = grid.getStore();
								store.removeAll();

								if (success) {
									var data = Ext.JSON
											.decode(response.responseText);
									store.add(data);
								}

								el.unmask();
							}
						});
			},


			getInfoPortal : function() {
				return {
					border : 0,
					html : "<h1>欢迎使用金海集团仓储管理系统</h1>"
				}
			},

			onWindowBeforeUnload : function(e) {
				return (window.event.returnValue = e.returnValue = '确认离开当前页面？');
			}
		});